package first;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LifeCycle extends HttpServlet{
	private FileWriter fw;
	private String nextLine=System.getProperty("line.separator");
	@Override
	public void init() throws ServletException{
		try {
			fw=new FileWriter("../logs/mylog/txt");
			fw.write("Call nit()method !");
			fw.write(nextLine);
			fw.flush();
		}catch(IOException ioe) {
			ioe.printStackTrace();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	@Override
	protected void service(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
		try {
			fw.write("Call protected service() method !");
			fw.write(nextLine);
			fw.flush();
		}catch(IOException ioe) {
			ioe.printStackTrace();
		}catch(Exception e) {
			e.printStackTrace();
		}
		super.service(req, resp);
	}
	@Override
	public void service(ServletRequest req,ServletResponse res) throws ServletException,IOException{
		try {
			fw.write("Call prublic service() method !");
			fw.write(nextLine);
			fw.flush();
		}catch(IOException ioe) {
			ioe.printStackTrace();
		}catch(Exception e) {
			e.printStackTrace();
		}
		super.service((HttpServletRequest)req, (HttpServletResponse)res);
	}
	@Override
	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		try {
			fw.write("Call doPost() method !");
			fw.write(nextLine);
			fw.flush();
		}catch(IOException ioe) {
			ioe.printStackTrace();
		}catch(Exception e) {
			e.printStackTrace();
		}
		response.setContentType("text/html;charset=gbk");
		PrintWriter out=response.getWriter();
		out.println("收到POST请求");
	}
	@Override
	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		try {
			fw.write("Call doGet() method !");
			fw.write(nextLine);
			fw.flush();
		}catch(IOException ioe) {
			ioe.printStackTrace();
		}catch(Exception e) {
			e.printStackTrace();
		}
		response.setContentType("text/html;charset=gbk");
		PrintWriter out=response.getWriter();
		out.println("收到GET请求");
	}
	@Override
	public void destroy() {
		try {
			fw.write("Call destroy() method !");
			fw.write(nextLine);
			fw.flush();
		}catch(IOException ioe) {
			ioe.printStackTrace();
		}catch(Exception e) {
			e.printStackTrace();
		}
		
	}

}
